/*
 * @(#)CommonService.java 2011-11-22
 *
 * Copyright 2011 北龙中网（北京）科技有限责任公司. All rights reserved.
 */
package cn.shangjia.shop.service.impl.common;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.shangjia.common.domain.SiteSetting;
import cn.shangjia.common.domain.User;
import cn.shangjia.common.domain.support.PostType;
import cn.shangjia.common.domain.support.State;
import cn.shangjia.common.domain.support.UserType;
import cn.shangjia.common.exceptions.Logger;
import cn.shangjia.shop.config.Constants;
import cn.shangjia.shop.dao.common.ShangjiaDao;
import cn.shangjia.shop.dao.common.ShopStatDao;
import cn.shangjia.shop.log.ShopLoggerFactory;
import cn.shangjia.shop.service.CompanyInfoService;
import cn.shangjia.shop.service.ShopInfoService;
import cn.shangjia.shop.service.UserInfoService;

/**
 * 公共服务
 * 
 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
 * @version 1.0, 2011-11-22 15:32:16
 * @since 1.0
 */
@Service
public class CommonService {
	
	private static final Logger logger = ShopLoggerFactory.getLogger(CommonService.class);
	
	@Resource
	private ShangjiaDao shangjiaDao;
	
	@Resource
	private ShopStatDao shopStatDao;
	
	@Resource
	private UserInfoService userInfoService;
	@Resource
	private CompanyInfoService companyInfoService;
	@Resource
	private ShopInfoService shopInfoService;
	
	/**
	 * 初始化 系统
	 * 
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	public void initShopSystem(int topNum) {
		
		if (topNum == 0)
			return ;

		List<String> shopIds = shopStatDao.getVisitTopShopIds(topNum);

		if (shopIds == null || shopIds.isEmpty())
			return;

		for (String userId : shopIds) {
			
			try  {
			
				// 加载用户
				User user = userInfoService.getUser(userId);
				if (user == null)
					continue; // 用户已经不存在
				if (!State.ACTIVATED.equals(user.getState()))
					continue; // 无效用户
		
				// 加载企业信息
				companyInfoService.getCompanyInfoByUserId(userId);
		
				// 加载商铺信息
				shopInfoService.findShopDirList(userId, PostType.BUY, 4); // 求购目录
				shopInfoService.findShopDirList(userId, PostType.PRODUCT, 4); // 供应目录
				shopInfoService.getShopByUserId(userId); // 商铺基础信息
				shopInfoService.getShopStatCodeByUserId(userId); // 统计代码
		
				// 用户联系人信息
				userInfoService.getUserContactOnline(userId);
				// 把所有用户名信息 再缓存一遍
				userInfoService.getUser(user.getUsername());
			
			} catch(Exception e) {
				logger.warn(e.getMessage());
			}
		}

	}
	
	/**
	 *  填充 GBT_MAP
	 * 
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	public void fillGBT_MAP() {
		synchronized (Constants.GBT_MAP) {
			
			Map<String, Map<String, String>> map = shangjiaDao.getSstGbtMap();
			for (String key : map.keySet()) {
				Constants.GBT_MAP.put(key, map.get(key).get("NAME"));
			}
			
		}
	}
	
	/**
	 *  填充商家网系统设置
	 * 
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	public void fillSiteSetting() {
		SiteSetting r = shangjiaDao.getSiteSetting();
		if (r != null) 
			synchronized (Constants.siteSetting) {
				Constants.siteSetting = r;
			}
	}
	
	/**
	 *  判断一个用户是否是vip用户
	 * 
	 * @param userId
	 * @return
	 * @author <a href="mailto:jinzhongze@knet.cn">jinzhongze</a>
	 */
	public boolean isVip(String userId) {
		Assert.notNull(userId, "userId 为空!");
		User user = userInfoService.getUser(userId);
		return UserType.VIP.equals(user.getUserType());
	}
}
